using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
//Touchless
using TouchlessLib;

namespace Puzzle_3D
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Puzzle : Microsoft.Xna.Framework.Game
    {
        private GraphicsDeviceManager graphics;
        private SpriteBatch spriteBatch;
        private Rectangle viewport;

        //Menu
        private Puzzle_Menu menu;
        private Puzzle_Game puzzleGame;

        private bool gameHasStarted;
        private bool isEndofGame;
        private int gameEndTime;

        private TouchlessMgr mgr;

        public float modelRotation = 0.0f;

        public Puzzle(TouchlessMgr _mgr)
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";

            this.mgr = _mgr;
            this.isEndofGame = false;
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here
            this.gameHasStarted = false;

            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);
            // TODO: use this.Content to load your game content here
            this.viewport = new Rectangle(0, 0, this.GraphicsDevice.Viewport.Width, this.GraphicsDevice.Viewport.Height);
            Window.Title = "3D-Puzzle";
            this.menu = new Puzzle_Menu(this, this.mgr);
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            this.modelRotation += (float)gameTime.ElapsedGameTime.TotalMilliseconds * MathHelper.ToRadians(0.1f);

            this.UpdateInput(gameTime);            

            // TODO: Add your update logic here
            if (!this.gameHasStarted)
            {
                this.menu.Update(gameTime);
            }

            base.Update(gameTime);
        }

        public bool EndOfGame
        {
            get
            {
                return this.isEndofGame;
            }
            set
            {
                this.isEndofGame = value;
            }
        }

        public int EndTime
        {
            get
            {
                return this.gameEndTime;
            }
            set
            {
                this.gameEndTime = value;
            }
        }

        private void UpdateInput(GameTime _time)
        {
            KeyboardState newState = Keyboard.GetState();

            if (newState.IsKeyDown(Keys.Space))
            {
                try
                {
                    this.menu.keyboardShortcut();
                    this.gameHasStarted = true;
                }
                catch (Exception e)
                { 
                    Console.WriteLine(e.ToString());
                }
            }
            else if (newState.IsKeyDown(Keys.V))
            {
                this.graphics.ToggleFullScreen();
            }
        }


        public void setIsGaming(bool _status)
        {
            this.gameHasStarted = _status;
        }

        public Rectangle getViewport()
        {
            return this.viewport;
        }

        public GraphicsDeviceManager getGraphics()
        {
            return this.graphics;
        }

        public SpriteBatch getSpriteBatch()
        {
            return this.spriteBatch;
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            // TODO: Add your drawing code here
            if (this.gameHasStarted)
            {                
                if (this.puzzleGame == null)
                {
                    this.mgr.CurrentCamera = null;
                    this.puzzleGame = new Puzzle_Game(this, this.menu.getGameImages(), gameTime);
                    this.puzzleGame.startCapturing();
                }
                if (this.isEndofGame && ((int)gameTime.TotalGameTime.TotalMilliseconds - this.EndTime >= 2000))
                {
                    if (this.isEndofGame && ((int)gameTime.TotalGameTime.TotalMilliseconds - this.EndTime >= 5000))
                    {
                        this.puzzleGame.endCapturing();
                        this.puzzleGame = null;
                        this.gameHasStarted = false;
                        this.EndOfGame = false;
                        this.menu.reset();
                        this.mgr.CurrentCamera = this.mgr.Cameras[0];
                    }
                    else
                    {
                        this.spriteBatch.Begin();
                        this.spriteBatch.Draw(this.Content.Load<Texture2D>("Graphic\\Ende"), new Rectangle(0, 0, 800, 600), Color.White);
                        this.spriteBatch.End();
                    }
                }
                else
                {
                    this.puzzleGame.Draw();
                }
            }
            else
            {
                this.menu.Draw();
            }
            base.Draw(gameTime);
        }
    }
}